﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;

namespace TomAndJerry
{

    class Level1Screen : LevelScreen
    {

        Texture2D backgroundTexture;
        Texture2D gridBgTexture;

        Texture2D helpBtnTexture;
        Texture2D exitBtnTexture;
        Texture2D testBridgeBtnTexture;
        Texture2D walkBtnTexture;
        Texture2D soundBtnTexture;
        Texture2D musicBtnTexture;
        Texture2D btn_matchTexture;
        Texture2D btn_atachTexture;
        Texture2D btn_deleteTexture;


        Texture2D bodyMatchTexture;
        Texture2D headMatchTexture;
        Texture2D greatPointTexture;

        Vector2 helpPosition;
        Vector2 exitPosition;
        Vector2 testBridgePosition;
        Vector2 walkPosition;
        Vector2 soundPosition;
        Vector2 musicPosition;
        Vector2 MatchPosition;


        InfoItem infoRope;
        bool isDrawing = true;

        List<ItemSprite> listItem;


        ItemSprite itemCurrent;

        /// <summary>
        /// Constructor.
        /// </summary>
        public Level1Screen()
            : base()
        {
            //sound effect for game
            //AudioManager.PushMusic("LoseTheme",false);
            this.Exiting += new EventHandler(Level1Screen_Exiting);
        }


        void Level1Screen_Exiting(object sender, EventArgs e)
        {
            //stop music
            //AudioManager.PopMusic();
            Debug.WriteLine("Debug: exiting");
        }

        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;

            backgroundTexture = content.Load<Texture2D>(@"Backgrounds\level1");
            helpBtnTexture = content.Load<Texture2D>(@"Items\help");
            exitBtnTexture = content.Load<Texture2D>(@"Items\exit");
            testBridgeBtnTexture = content.Load<Texture2D>(@"Items\test");
            soundBtnTexture = content.Load<Texture2D>(@"Items\sound_ena");
            musicBtnTexture = content.Load<Texture2D>(@"Items\music_ena");
            btn_atachTexture = content.Load<Texture2D>(@"Items\btn_atach");
            btn_deleteTexture = content.Load<Texture2D>(@"Items\Delete");
            btn_matchTexture = content.Load<Texture2D>(@"Items\btn_match");

            bodyMatchTexture = content.Load<Texture2D>(@"Items\bodyMatch");
            headMatchTexture = content.Load<Texture2D>(@"Items\headMatch");
            greatPointTexture = content.Load<Texture2D>(@"Items\greatPoint");

            helpPosition = new Vector2(80, 700);
            exitPosition = new Vector2(80,745);
            testBridgePosition = new Vector2(480,15);
            soundPosition = new Vector2(80, 2);
            musicPosition = new Vector2(80, 60);
            MatchPosition = new Vector2(100, 200);

            infoRope = new InfoItem();
            listItem = new List<ItemSprite>();
            itemCurrent = new Matches(bodyMatchTexture, headMatchTexture);
        }

        /// <summary>
        /// UnloadContent
        /// </summary>

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        /// <summary>
        /// Handles user input.
        /// </summary>
        public override void HandleInput()
        {
            if (!isDrawing)
            {
                int clickMode = 0;
                ///back click
                if (InputManager.IsButtonClicked(new Rectangle(
                    (int)helpPosition.X - helpBtnTexture.Height, (int)helpPosition.Y,
                    (int)(helpBtnTexture.Height),
                    (int)(helpBtnTexture.Width))))
                {
                    clickMode = 1;
                    Debug.WriteLine("Touch");
                }

                ///exit click
                else if (InputManager.IsButtonClicked(new Rectangle(
                    (int)exitPosition.X - exitBtnTexture.Height, (int)exitPosition.Y,
                    (int)(exitBtnTexture.Height),
                    (int)(exitBtnTexture.Width))))
                {
                    clickMode = 2;
                    Debug.WriteLine("Touch");
                }

                ///right click
                else if (InputManager.IsButtonClicked(new Rectangle(
                    (int)testBridgePosition.X - testBridgeBtnTexture.Height, (int)testBridgePosition.Y,
                    (int)(testBridgeBtnTexture.Height),
                    (int)(testBridgeBtnTexture.Width))))
                {
                    clickMode = 3;
                    Debug.WriteLine("Touch");
                }


                ///sound click
                else if (InputManager.IsButtonClicked(new Rectangle(
                    (int)soundPosition.X - soundBtnTexture.Height, (int)soundPosition.Y,
                    (int)(soundBtnTexture.Height),
                    (int)(soundBtnTexture.Width))))
                {
                    clickMode = 4;
                    Debug.WriteLine("Touch");
                }

                ///music click
                else if (InputManager.IsButtonClicked(new Rectangle(
                    (int)musicPosition.X - musicBtnTexture.Height, (int)musicPosition.Y,
                    (int)(musicBtnTexture.Height),
                    (int)(musicBtnTexture.Width))))
                {
                    clickMode = 5;
                    Debug.WriteLine("Touch");
                }
                else if (InputManager.IsButtonClicked(new Rectangle(0, 0, 480, 800)))
                {
                    Debug.WriteLine("draw");

                    isDrawing = true;
                }

                GameScreen screen = null;
                switch (clickMode)
                {
                    case 1://back home
                        screen = new HomeScreen();
                        break;
                    case 2://help arrow

                        break;
                    case 3://exit arrow
                        screen = new Instruction2();
                        break;
                    case 4:
                        ///<<<test bridge>>>
                        break;
                    case 5:
                        ///<<<walk>>>
                        break;
                    case 6://sound
                        ///<<enable/disable sound>>
                        break;
                    case 7://music
                        ///<<enable/disable music>>
                        break;
                    default:
                        break;
                }
                if (screen != null)
                {
                    ExitScreen();
                    ScreenManager.AddScreen(screen);
                }
            }
            else
            {
                itemCurrent = InputManager.DrawTouchGesture(itemCurrent);
                if (itemCurrent.infor.Mode == 2)
                {
                    listItem.Add(itemCurrent);
                    itemCurrent = new Matches(bodyMatchTexture, headMatchTexture);
                }

            }
            // Debug.WriteLine(infoRope.Angle+"------------");
            //CheckTouchGesture();
            base.HandleInput();
        }


        private void CheckTouchGesture()
        {
            var touchCol = TouchPanel.GetState();

            foreach (var touch in touchCol)
            {
                // You're looking for when they finish a drag, so only check
                // released touches.
                if (touch.State != TouchLocationState.Released)
                    continue;

                TouchLocation prevLoc;

                // Sometimes TryGetPreviousLocation can fail. Bail out early if this happened
                // or if the last state didn't move
                if (!touch.TryGetPreviousLocation(out prevLoc) || prevLoc.State != TouchLocationState.Moved)
                    continue;

                // get your delta
                var delta = touch.Position - prevLoc.Position;
                Debug.WriteLine("touch "+"-------------" + delta.X +"===="+ delta.Y);
                // Usually you don't want to do something if the user drags 1 pixel.
                if (delta.LengthSquared() < 2025f)
                    continue;

                /*if (delta.X < 0)
                    gameVm.MoveLeft(Math.Abs((int)delta.X));
                else if (delta.X > 0)
                    gameVm.MoveRight((int)delta.X);
                else if (delta.Y > 0)
                    gameVm.MoveDown((int)delta.Y);*/
            }
        }




        /// <summary>
        /// Updates the menu.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }


        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            ScreenManager.GraphicsDevice.SetRenderTarget(ScreenManager.FinalImageTarget);
            ScreenManager.GraphicsDevice.Clear(Color.CornflowerBlue);

            // we will do all of our drawing here
            ScreenManager.GraphicsDevice.SetRenderTarget(null);

            spriteBatch.Begin();
            spriteBatch.Draw(backgroundTexture, new Vector2(480, 0), null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(helpBtnTexture, helpPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(exitBtnTexture, exitPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(testBridgeBtnTexture, testBridgePosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(soundBtnTexture, soundPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(musicBtnTexture, musicPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);

            foreach (ItemSprite item in listItem)
            {
                if (item.isActive)
                {
                    ((Matches) item).Draw(spriteBatch);
                }
            }
            if (itemCurrent.infor.Mode != 0)
            {
                //spriteBatch.Draw(itemCurrent.itemTexture, itemCurrent.infor.Pos1, null, Color.White, itemCurrent.infor.Angle, Vector2.Zero, itemCurrent.infor.Scale, SpriteEffects.None, 0f);
                ((Matches)itemCurrent).Draw(spriteBatch);
            }
            spriteBatch.End();
        }
    }
}
